package com.hq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.pojo.DDdan;
import com.hq.service.DDService;
import com.hq.util.FileUploadUtil;
import com.hq.util.SnowflakeIdUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DDController {
    @Resource
    DDService ddService;

    @RequestMapping("/list.do")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum, Model model,
                       String dddatep,
                       String dddatex,
                       String dddated,
                       String ddbh,
                       String name,
                       String yxj,
                       String pricex,
                       String priced,
                       String beizhu,
                       String lxr,
                       String address,
                       String phone,
                       String gh,
                       String zdname,
                       String zddatex,
                       String zddated
                       ){
        Page<Object> page = PageHelper.startPage(pageNum, 4);
        List list=ddService.list(dddatep,
                dddatex,
                dddated,
                ddbh,
                name,
                yxj,
                pricex,
                priced,
                beizhu,
                lxr,
                address,
                phone,
                gh,
                zdname,
                zddatex,
                zddated);
        model.addAttribute("list",list);
        model.addAttribute("dddatep",dddatep);
        model.addAttribute("dddatex",dddatex);
        model.addAttribute("dddated",dddated);
        model.addAttribute("ddbh",ddbh);
        model.addAttribute("name",name);
        model.addAttribute("yxj",yxj);
        model.addAttribute("pricex",pricex);
        model.addAttribute("priced",priced);
        model.addAttribute("beizhu",beizhu);
        model.addAttribute("lxr",lxr);
        model.addAttribute("address",address);
        model.addAttribute("phone",phone);
        model.addAttribute("gh",gh);
        model.addAttribute("zdname",zdname);
        model.addAttribute("zddatex",zddatex);
        model.addAttribute("zddated",zddated);
        model.addAttribute("page",page);
        return "list";
    }

    @RequestMapping("/addy.do")
    public String addy(HttpSession session){
        String s = new SnowflakeIdUtils(1, 1).nextId() + "";
        session.setAttribute("s",s);
        return "add";
    }

    @RequestMapping("/adddingdan.do")
    @ResponseBody
    public int adddingdan(DDdan dDdan, MultipartFile photo,HttpSession session){
        if(photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            dDdan.setFujian(upload);
        }
        return ddService.adddingdan(dDdan);
    }

    @RequestMapping("/delids.do")
    @ResponseBody
    public int delids(String id){
        return ddService.delids(id);
    }
}
